import * as React from "solar/w3ts/tsx";
import BaseUtil from "../solar/util/BaseUtil";
import ForceUtil from "../solar/util/ForceUtil";
import SelectUtil from "../solar/util/SelectUtil";

type configDataType = {
    key?: string,
    name?: string,
    enable_value?: number,
    map_level?: number,
    ability_id?: string
}
// DzLoadToc('UI\\UiItemArchives.toc')
export default class UiAbilityArchives {

    static config: (configDataType & any)[] = []
    static AwardArchivesAbility2UnitType = ""//hpea

    bg_root = {visible: false, ref: {current: 0}};

    btn_close = {visible: true, ref: {current: 0}};

    btn_kai_guan = {visible: true, ref: {current: 0}};

    bd_tip_root = {visible: false, ref: {current: 0}};

    bd_icon = {visible: true, ref: {current: 0}};

    text_name = {visible: true, ref: {current: 0}};

    text_tip = {visible: true, ref: {current: 0}};

    text_ubertip = {visible: true, ref: {current: 0}};

    constructor(container = DzGetGameUI()) {
        React.render(this.render(), container);
        this.awardArchivesAbility();
    }


    onClick_btn_close() {
        //隐藏节点 btn_close
        this.bg_root.visible = false;
        DzFrameShow(this.bg_root.ref.current, this.bg_root.visible);
    }


    onTip_bd_tip_root(tipHandle: number, triggerHandle: number, cfgData: configDataType) {
        let obj = ability[cfgData.ability_id];
        if (!obj) {
            log.warn("技能物遍不存在:" + cfgData.ability_id)
            return
        }
        DzFrameSetText(this.text_tip.ref.current, obj.Name)
        let map_level = DzAPI_Map_GetMapLevel(GetLocalPlayer());
        let storedInteger = DzAPI_Map_GetStoredInteger(GetLocalPlayer(), cfgData.key);
        if (storedInteger && storedInteger >= cfgData.enable_value && map_level >= cfgData.map_level) {
            DzFrameSetText(this.text_name.ref.current, "已解锁")
        } else {
            DzFrameSetText(this.text_name.ref.current, "解锁要求|n" + cfgData.name + ":x" + cfgData.enable_value + "|n地图等级" + cfgData.map_level)
        }

        let tip = obj.Ubertip1 ? obj.Ubertip1 : obj.Ubertip;
        DzFrameSetText(this.text_ubertip.ref.current, tip ? tip : obj.Name)
        DzFrameSetTexture(this.bd_icon.ref.current, obj.Art, 0)
    }


    onClick_btn_kai_guan() {
        //切换节点可见 btn_kai_guan
        this.bg_root.visible = !this.bg_root.visible;
        DzFrameShow(this.bg_root.ref.current, this.bg_root.visible);
    }


    /**
     * 奖励存档效果
     *
     */
    awardArchivesAbility() {
        if (UiAbilityArchives.AwardArchivesAbility2UnitType && UiAbilityArchives.AwardArchivesAbility2UnitType.length == 4) {
            BaseUtil.runLater(2, () => {
                ForceUtil.forUserForce(player => {
                    SelectUtil.forPlayerUnits(u => {
                        for (let cfgData of UiAbilityArchives.config) {
                            let storedInteger = DzAPI_Map_GetStoredInteger(player, cfgData.key);
                            let map_level = DzAPI_Map_GetMapLevel(player);
                            if (storedInteger && storedInteger >= cfgData.enable_value && map_level >= cfgData.map_level) {
                                UnitAddAbility(u, FourCC(cfgData.ability_id))
                            }
                        }
                    }, GetPlayerId(player), UiAbilityArchives.AwardArchivesAbility2UnitType);
                });
            })
            return
        }

        BaseUtil.runLater(2, () => {
            ForceUtil.forUserForce(player => {
                let hero = SelectUtil.getAnHero(GetPlayerId(player));
                if (!IsHandle(hero)) {
                    return;
                }
                for (let cfgData of UiAbilityArchives.config) {
                    let storedInteger = DzAPI_Map_GetStoredInteger(player, cfgData.key);
                    let map_level = DzAPI_Map_GetMapLevel(player);
                    if (storedInteger && storedInteger >= cfgData.enable_value && map_level >= cfgData.map_level) {
                        UnitAddAbility(hero, FourCC(cfgData.ability_id))
                    }
                }

            });
        }, 30)
    }

    /**
     * 显示存档激活效果
     */
    getItems() {

        let allItem = [];

        for (let i = 0; i < UiAbilityArchives.config.length; i++) {
            let cfgData = UiAbilityArchives.config[i];

            let x = 0.02 + (0.08 * (i % 8))
            let y = 0.28 - (0.08 * Math.floor(i / 8))

            let obj = ability[cfgData.ability_id];
            if (!obj) {
                log.warn("技能物遍不存在:" + cfgData.ability_id)
                continue
            }
            let bg = obj.Art;
            let isEnable = false;
            let map_level = DzAPI_Map_GetMapLevel(GetLocalPlayer());
            let storedInteger = DzAPI_Map_GetStoredInteger(GetLocalPlayer(), cfgData.key);
            if (storedInteger && storedInteger >= cfgData.enable_value && map_level >= cfgData.map_level) {
                isEnable = true;
            }
            let alpha = 255
            //没有获取时的 暗图标
            if (!isEnable) {
                let ebg = "__temp_bg_" + bg
                EXBlendButtonIcon("DisICO.blp", bg, ebg)
                bg = ebg;
                alpha = 150;
            }
            allItem.push(<button
                id="item"
                inherits="ScoreScreenTabButtonTemplate"
                background-image={bg}
                position={{x, y}}
                size={{width: 0.04, height: 0.04}}
                alpha={alpha}
                frametip={{
                    frametipRef: this.bd_tip_root.ref, pos: 7, onTip: (fh, th) => {
                        this.onTip_bd_tip_root(fh, th, cfgData)
                    }
                }}
            >
            </button>)
        }
        return allItem
    }

    render() {

        let allItem = this.getItems();

        return (<root>
            <backdrop
                id="bg_root"
                inherits="bg_root"
                position={{x: 0.084, y: 0.197}}
                size={{width: 0.631, height: 0.334}}
                visible={this.bg_root.visible}
                ref={this.bg_root.ref}
                texture="big.blp"
            >
                <button
                    id="btn_close"
                    inherits="ScoreScreenTabButtonTemplate"
                    position={{x: 0.627, y: 0.332}}
                    size={{width: 0.020, height: 0.023}}
                    visible={this.btn_close.visible}
                    ref={this.btn_close.ref}
                    background-image={"mall_tips_close.blp"}
                    onClick={() => this.onClick_btn_close()}
                >
                </button>
                {allItem}
            </backdrop>

            <button
                id="btn_kai_guan"
                inherits="ScoreScreenTabButtonTemplate"
                position={{x: 0.050, y: 0.533}}
                size={{width: 0.041, height: 0.041}}
                visible={this.btn_kai_guan.visible}
                ref={this.btn_kai_guan.ref}
                background-image={"tut_01.blp"}
                onClick={() => this.onClick_btn_kai_guan()}
            >
            </button>

            <backdrop
                id="bd_tip_root"
                inherits="bd_tip_root"
                position={{x: 0.000, y: 0.002}}
                size={{width: 0.2, height: 0.25}}
                visible={this.bd_tip_root.visible}
                ref={this.bd_tip_root.ref}
                texture="big.blp"
            >
                <backdrop
                    id="bd_icon"
                    inherits="bd_icon"
                    position={{x: 0.001, y: 0.2}}
                    size={{width: 0.041, height: 0.040}}
                    visible={this.bd_icon.visible}
                    ref={this.bd_icon.ref}
                >
                </backdrop>
                <text
                    id="text_name"
                    inherits="text_name"
                    position={{x: 0.000, y: 0.085}}
                    size={{width: 0.06, height: 0.1}}
                    visible={this.text_name.visible}
                    textAlignment={FRAMEPOINT_TOP}
                    ref={this.text_name.ref}
                    text="文本"
                >
                </text>
                <text
                    id="text_tip"
                    inherits="text_tip"
                    position={{x: 0.076, y: 0.21}}
                    size={{width: 0.105, height: 0.024}}
                    visible={this.text_tip.visible}
                    textAlignment={FRAMEPOINT_TOP}
                    ref={this.text_tip.ref}
                    text="文本"
                >
                </text>
                <text
                    id="text_ubertip"
                    inherits="text_ubertip"
                    position={{x: 0.062, y: 0.013}}
                    size={{width: 0.132, height: 0.229}}
                    textAlignment={TEXTALIGN_CENTER}
                    visible={this.text_ubertip.visible}
                    ref={this.text_ubertip.ref}
                    text="文本"
                >
                </text>
            </backdrop>

        </root>);

    }
}
